
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(document.URL);
    if (results == null) return "";
    else return results[1];
}

function onText(status, station) {
    var responseString;
    var fail = true;
    if (status == 'no embed') responseString = title + ' cannot be added to JetTube! This video does not allow embedding.';
    else if (status == 'duplicate') responseString = title + ' cannot be added to JetTube! This video already exists.';
    else if (status == 'success') {
        responseString = title + " added to " + station + " Station on The JetTube!";
        fail = false;
    }
    else
    {
    responseString = title + ' cannot be added to JetTube! There was a server error.';
    }
    var colorchoice;
    if (fail) {
        colorchoice = '#bbb';
    }
    else {
        colorchoice = '#36b';
    }
    // Create the overlay at the top of the page and fill it with data.
    var trends_dom = document.createElement('div');
    var text_dom = document.createTextNode(responseString);
    trends_dom.appendChild(text_dom);
    trends_dom.style.background = colorchoice;
    trends_dom.style.color = '#fff';
    trends_dom.style.padding = '10px';
    trends_dom.style.position = 'relative';
    trends_dom.style.zIndex = '123456';
    trends_dom.style.font = '14px Arial';
    document.body.insertBefore(trends_dom, document.body.firstChild);
}


var comPort = chrome.extension.connect({name:'jettube'});
var videoID = gup('v');
var title = document.title.substring(10);
comPort.onMessage.addListener(function (msg) {
    onText(msg.status, msg.station);
});
comPort.postMessage({videoID:videoID,title:title});